
var userInfoJson = COOKIE_TOOL.getCookie("userInfo");
if (window.MathFun) {
    if (window.MathFun.getUserInfo()) {
        userInfoJson = window.MathFun.getUserInfo();
    }
}

var AjaxUtil = {
    postWithJson: function (url, params, callback,callBackWrong) {
        var headers = {
            'content-type': "application/json"
        }
        if (!userInfoJson) {
            if (window.MathFun) {
                if (window.MathFun.getUserInfo()) {
                    userInfoJson = window.MathFun.getUserInfo();
                }
            } else {
                userInfoJson = COOKIE_TOOL.getCookie("userInfo");
            }
        }

        if (userInfoJson) {
            var userInfo = JSON.parse(userInfoJson)
            var oauth2TokenInfo = JSON.parse(userInfo.oauth2TokenInfo);
            headers["Authorization"] = oauth2TokenInfo.token_type + " " + oauth2TokenInfo.access_token;
        }

        $.ajax({
            type: "POST",
            async: false,
            data: JSON.stringify(params),
            headers: headers,
            url: url,
            dataType: "json",
            success: function (result) {
                //console.log(result);
                if (result.code == 200) {
                    callback(result.result)
                }else {
                    if (callBackWrong){
                        callBackWrong(result.result)
                    }
                }
            },
            error: function (e) {
               console.log("error: " + JSON.stringify(e))
            }
        });
    },
    get: function (url, params, callback) {
        var headers = {}
        if (!userInfoJson) {
            if (window.MathFun) {
                if (window.MathFun.getUserInfo()) {
                    userInfoJson = window.MathFun.getUserInfo();
                }
            } else {
                userInfoJson = COOKIE_TOOL.getCookie("userInfo");
            }
        }

        if (userInfoJson) {
            var userInfo = JSON.parse(userInfoJson)
            var oauth2TokenInfo = JSON.parse(userInfo.oauth2TokenInfo);
            headers["Authorization"] = oauth2TokenInfo.token_type + " " + oauth2TokenInfo.access_token;
        }

        $.ajax({
            type: "GET",
            url: url,
            async: false,
            data: params ? params : "",
            headers: headers,
            success: function (result) {
                callback(result)
            },
            error: function (e) {
               console.log("error: " + JSON.stringify(e))
            }
        });
    },
    posJson: function (url, params, callback, async=true) {
        var headers = {
            'content-type': "application/json"
        }
        if (!userInfoJson) {
            if (window.MathFun) {
                if (window.MathFun.getUserInfo()) {
                    userInfoJson = window.MathFun.getUserInfo();
                }
            } else {
                userInfoJson = COOKIE_TOOL.getCookie("userInfo");
            }
        }

        if (userInfoJson) {
            var userInfo = JSON.parse(userInfoJson)
            var oauth2TokenInfo = JSON.parse(userInfo.oauth2TokenInfo);
            headers["Authorization"] = oauth2TokenInfo.token_type + " " + oauth2TokenInfo.access_token;
        }

        $.ajax({
            type: "POST",
            async: async,
            data: JSON.stringify(params),
            headers: headers,
            url: url,
            dataType: "json",
            success: function (result) {
                callback(result)
            },
            error: function (e) {
               console.log("error: " + JSON.stringify(e))
            }
        });
    },
    putJson: function (url, params, callback) {
        var headers = {
            'content-type': "application/json"
        }
        if (!userInfoJson) {
            if (window.MathFun) {
                if (window.MathFun.getUserInfo()) {
                    userInfoJson = window.MathFun.getUserInfo();
                }
            } else {
                userInfoJson = COOKIE_TOOL.getCookie("userInfo");
            }
        }

        if (userInfoJson) {
            var userInfo = JSON.parse(userInfoJson)
            var oauth2TokenInfo = JSON.parse(userInfo.oauth2TokenInfo);
            headers["Authorization"] = oauth2TokenInfo.token_type + " " + oauth2TokenInfo.access_token;
        }

        $.ajax({
            type: "PUT",
            async: false,
            data: JSON.stringify(params),
            headers: headers,
            url: url,
            dataType: "json",
            success: function (result) {
                callback(result)
            },
            error: function (e) {
                console.log("error: " + JSON.stringify(e))
            }
        });
    },
    post: function (url, params, callback) {
        var headers = {}
        if (!userInfoJson) {
            if (window.MathFun) {
                if (window.MathFun.getUserInfo()) {
                    userInfoJson = window.MathFun.getUserInfo();
                }
            } else {
                userInfoJson = COOKIE_TOOL.getCookie("userInfo");
            }
        }

        if (userInfoJson) {
            var userInfo = JSON.parse(userInfoJson)
            var oauth2TokenInfo = JSON.parse(userInfo.oauth2TokenInfo);
            headers["Authorization"] = oauth2TokenInfo.token_type + " " + oauth2TokenInfo.access_token;
        }

        $.ajax({
            type: "POST",
            async: false,
            data: JSON.stringify(params),
            headers: headers,
            url: url,
            dataType: "json",
            success: function (result) {
                callback(result)
            },
            error: function (e) {
               console.log("error: " + JSON.stringify(e))
            }
        });
    },

    getGameInfo: function (handler) {
        var gameProjectId = TOOL.getQueryVariable("gameProjectId");
        var gradeId = TOOL.getQueryVariable("gradeId");

        var param = {"gameProjectId": gameProjectId, "gradeId": gradeId};
        AjaxUtil.postWithJson(GLOBAL_VAR.api() + '/project/game/info', param, function (data) {
            handler(data);
        });
    },

    getUserInfo: function (handler) {
        if (!userInfoJson || userInfoJson == '') {
            if (window.MathFun) {
                if (window.MathFun.getUserInfo()) {
                    userInfoJson = window.MathFun.getUserInfo("userInfoArm");
                }
            } else if(window.MagicPuzzle){
                if (window.MagicPuzzle.getUserInfo()) {
                    userInfoJson = window.MagicPuzzle.getUserInfo();
                }
            } else {
                userInfoJson = COOKIE_TOOL.getCookie("userInfo");
            }
        }

        var userInfo;
        if (userInfoJson) {
            userInfo = JSON.parse(userInfoJson)
            if(COOKIE_TOOL.getCookie("classRoomList")){
                userInfo["classroomList"] = JSON.parse(COOKIE_TOOL.getCookie("classRoomList"));
            }
        }
        if( window.location.pathname.toLowerCase()=="/middle/armmove.html"){
            userInfoJson = COOKIE_TOOL.getCookie("userInfoArm");
            if (userInfoJson){
                userInfo = JSON.parse(userInfoJson)
            }else {
                userInfo=null;
            }

        }
        handler(userInfo);
    },
    getAsync: function (url, params, callback) {
        var headers = {}
        if (!userInfoJson) {
            if (window.MathFun) {
                if (window.MathFun.getUserInfo()) {
                    userInfoJson = window.MathFun.getUserInfo();
                }
            } else {
                userInfoJson = COOKIE_TOOL.getCookie("userInfo");
            }
        }

        if (userInfoJson) {
            var userInfo = JSON.parse(userInfoJson)
            var oauth2TokenInfo = JSON.parse(userInfo.oauth2TokenInfo);
            headers["Authorization"] = oauth2TokenInfo.token_type + " " + oauth2TokenInfo.access_token;
        }

        $.ajax({
            type: "GET",
            url: url,
            async: true,
            data: params ? params : "",
            headers: headers,
            success: function (result) {
                callback(result)
            },
            error: function (e) {
                console.log("error: " + JSON.stringify(e))
            }
        });
    }
};
